/*
 * to use online script you can use jquery getScript plugin as follows:
 *
 * $.getScript('http://sisense.com/abc.js', function(data, textStatus)
 * {
 *		// your code which requires abc.js here
 * });
 *
 * // more info at: http://api.jquery.com/jQuery.getScript/
 *
 */


/*
 * set to true when replacing widget rendering engine.
 */
this.isCustom = false;

/*
 * exectuted after the widget controller was initialized, useful when overriding entire widget in order to instantiate entirely different implementation.
 *
 * @param {args} wraps all function arguments, including widget, dashboard and widget controller instances.
 *
 */
this.init = function (args) {

};

/*
 * executed before the widget query result is loaded to the DOM and allows canceling such beavior and replace it with custom widget implementation.
 * to cancel the default population just set 'args.cancel = true' and the widget default population will be ignored.
 *
 * @param {args} wraps all function arguments, including query result, widget, dashboard and widget controller instances.
 *
 */
this.beforeResultLoaded = function (args) {


};

/*
 * executed after the widget result were added to the DOM. useful to apply DOM tweaks after query result population.
 *
 * @param {args} wraps all function arguments, including query result, widget, dashboard and widget controller instances.
 *
 */
this.afterResultLoaded = function (args) {

    // 1. Set options
    var options = {

        imageColumn: 'Transportation',
        replaceValues: true
    };

    // 2. Define image rules
    var imageMap = {

        "Bycicle": '/images/transport/bycicle.png',
        "Bus": '/images/transport/bus.png',
        "Car": '/images/transport/car.png',
        "Airplane": '/images/transport/airplane.png'
    };

    var spans =  $($(args.result.table).find('[field="' + options.imageColumn + '"] span')),
        colText = undefined,
        imgName = undefined;

    for (var i = 1; i < spans.length; i ++) {

        colText = $(spans[i]).html();
        imgName = imageMap[colText];

        imgDiv = $('<img src="' + imgName + '" alt="Arrow" width="auto" height="auto" max-width="18px" max-height="18px" />');

        if (options.replaceValues)
            imgName ? $(spans[i]).replaceWith(imgDiv) : $(spans[i]).remove();
        else
            if(imgName) $(imgDiv).insertBefore($(spans[i]));
    }
};

/*
 * exectuted after all dashboard widget models were initialiezed. useful for binding with other widget events.
 *
 * @param {args} wraps all function arguments, including widget, dashboard and widget controller instances.
 *
 */
this.handleDashboardInitialized = function (args) {
};
